﻿import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrganizationUnitDto, ProfileServiceProxy, UserEditDto, UserServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { StandardPartsDetailDto,StandardPartsFeatureDto } from '@shared/service-proxies/service-proxies';
import { StandardPartsDetailServiceProxy,StandardDetailType } from '@shared/service-proxies/service-proxies';
@Component({
    selector: 'createOrEditCodeBaseModal',
    templateUrl: './create-or-edit-code-base-modal.component.html',
    styles: [`.user-edit-dialog-profile-image {
             margin-bottom: 20px;
        }`
    ]
})
export class CreateOrEditCodeBaseModalComponent extends AppComponentBase {

    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    canChangeUserName = true;
    standardPartsDetail: StandardPartsDetailDto = new StandardPartsDetailDto();
    user: UserEditDto = new UserEditDto();
    profilePicture: string;

    allOrganizationUnits: OrganizationUnitDto[];
    memberedOrganizationUnits: string[];
    standardPartsDetailIsTemperingOption:object[];// 是否热处理
    standardTypes:Array<object>;
    editFlag:boolean;
    
    constructor(
        injector: Injector,
        private _userService: UserServiceProxy,
        private _standardPartsDetailServiceProxy: StandardPartsDetailServiceProxy,
        private _profileService: ProfileServiceProxy
    ) {
        super(injector);
        this.standardPartsDetailIsTemperingOption = new Array();
        this.standardPartsDetailIsTemperingOption.push({value:true,text:"是"});
        this.standardPartsDetailIsTemperingOption.push({value:false,text:"否"});

        this.standardTypes = new Array<object>()
        this.standardTypes.push({"value":0,"text":StandardDetailType[0]});
        this.standardTypes.push({"value":1,"text":StandardDetailType[1]});
    }

    show(standardPartsDetail?: StandardPartsDetailDto): void {
        if(standardPartsDetail){
            this.standardPartsDetail=standardPartsDetail;
            this.editFlag = true;
        } else {
            this.standardPartsDetail=new StandardPartsDetailDto();
            this.standardPartsDetail.features=new StandardPartsFeatureDto();
            this.editFlag = false;
        }
        this.active=true;
        this.modal.show();
    }

    getProfilePicture(profilePictureId: string): void {
        if (!profilePictureId) {
            this.profilePicture = this.appRootUrl() + 'assets/common/images/default-profile-picture.png';
        } else {
            this._profileService.getProfilePictureById(profilePictureId).subscribe(result => {

                if (result && result.profilePicture) {
                    this.profilePicture = 'data:image/jpeg;base64,' + result.profilePicture;
                } else {
                    this.profilePicture = this.appRootUrl() + 'assets/common/images/default-profile-picture.png';
                }
            });
        }
    }
    onShown(): void {
        document.getElementById('code').focus();
    }
    save(): void {
        this.saving = true;
        if(!this.editFlag){
            this._standardPartsDetailServiceProxy.createDetail(this.standardPartsDetail)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe(() => {
                    this.notify.info("SavedSuccessfully");
                    this.close();
                    this.modalSave.emit(null);
                });
        }else{
            this._standardPartsDetailServiceProxy.updateDetail(this.standardPartsDetail)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe(() => {
                    this.notify.info(this.l('SavedSuccessfully'));
                    this.close();
                    this.modalSave.emit(null);
                });
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
